import {message} from 'antd';

import {query as queryUsers, remove, addUser, status, queryCurrent} from '../services/user';

export default {
    namespace: 'admin',

    state: {
        data: {list: [], pagination: {size: 10, current: 1, total: 0}},
        loading: false,
        currentUser: {},
        created_at: [],
        confirmLoading: false,
        modalVisible: false,
    },

    effects: {
        * fetch({ payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryUsers, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        *status({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(status, payload);
           if (result.status) {
               message.success('修改成功');
               yield put({
                    type: 'fetch',
                });
               if (callback) {
                   callback();
               }
           } else {
               message.error('修改失败');
           }
        },
        *add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeCreateLoading',
                payload: true,
            });
            const result = yield call(addUser, payload);
            if (callback) {
                callback(result);
            }
            yield put({
                type: 'changeCreateLoading',
                payload: false,
            });
        },
        *remove({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(remove, payload);
            if (result.status) {
                message.success('删除成功');
                yield put({
                    type: 'fetch',
                });
            } else {
                message.error('删除失败');
            }
        },
        *fetchCurrent(_, {call, put}) {
            const response = yield call(queryCurrent);
            yield put({
                type: 'saveCurrentUser',
                payload: response.data,
            });
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: {list: action.payload.data, pagination: action.payload.page},
            };
        },
        changeCreateLoading(state, action) {
            return {
                ...state,
                confirmLoading: action.payload,
            };
        },
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
        changeVisible(state, action) {
            return {
                ...state,
                modalVisible: action.payload,
            };
        },
        saveCurrentUser(state, action) {
            return {
                ...state,
                currentUser: action.payload,
            };
        },
        changeNotifyCount(state, action) {
            return {
                ...state,
                currentUser: {
                    ...state.currentUser,
                    notifyCount: action.payload,
                },
            };
        },
    },
};
